//
//  CloudClassCloudPayAliViewController.swift
//  FXFSOnlineEdu
//
//  Created by xuejiaodong on 1/9/19.
//  Copyright © 2019 湖南新云网科技有限公司. All rights reserved.
//

import UIKit
import WebKit

@objcMembers
class CloudClassCloudPayViewController: UIViewController,WKUIDelegate {
    
   
    var payWebView: WKWebView!
    
    var isSuccess = false;
    
    private var orderUrl:String {
        get {
            let url = UserDefaults().string(forKey:"url") ;
            return url!;
        }
    }
    
   @objc func back() ->Void{
           self.dismiss(animated: true, completion: nil)
       }
       
       override func viewDidLoad() {
           
           super.viewDidLoad()
           let webConfiguration = WKWebViewConfiguration()

           //设置视频是否需要用户手动播放  设置为NO则会允许自动播放
           if #available(iOS 9.0, *) {
               webConfiguration.requiresUserActionForMediaPlayback = true
           } else {
               // Fallback on earlier versions
           }
           payWebView = WKWebView(frame: .zero, configuration: webConfiguration)
          payWebView!.uiDelegate = self
                  payWebView.scrollView.isScrollEnabled=false
                  payWebView!.navigationDelegate = self
           self.view.addSubview(payWebView)
           self.view.backgroundColor=UIColor.white
            openAli(orderUrl)
           
       }
    
}


extension CloudClassCloudPayViewController: WKNavigationDelegate {
    
    //
    func buildAliView() {
        payWebView.frame = self.view.bounds
        payWebView.isHidden = true
        payWebView.navigationDelegate = self
        self.view.addSubview(payWebView)
    }
    

    func openAli(_ url: String) {
        
        let userDefaule = UserDefaults.standard
        userDefaule.synchronize()
        payWebView.isHidden = false
        loadWithUrlStr(url: url)
        
    }
    
    func loadWithUrlStr(url: String) {
        if url.count > 0{
            DispatchQueue.main.async {
                let request = URLRequest.init(url: URL.init(string: url)!, cachePolicy: URLRequest.CachePolicy.returnCacheDataElseLoad, timeoutInterval: 30)
                self.payWebView.load(request)
            }
        }
    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if self.webView(shouldStartLoadWith: navigationAction.request) {
            decisionHandler(WKNavigationActionPolicy.allow)
        } else {
            decisionHandler(WKNavigationActionPolicy.cancel)
        }
    }

   public func webView(shouldStartLoadWith request: URLRequest) -> Bool {
//        if let url = request.url?.absoluteString {
//            let isIntercepted = AlipaySDK.defaultService().payInterceptor(withUrl: url, fromScheme: "zhuozhangtearchs") { (result) in
//                if let dic = result as [AnyHashable:AnyObject]?{
//                    if let success = dic["isProcessUrlPay"] {
//
//                        if success.intValue == 1{
//
//                            let urlStr = result?["returnUrl"] as? String
//
//                            if let url = urlStr {
//                                self.loadWithUrlStr(url: url)
//                            }
//
//
//                            NotificationCenter.default.post(name:Notification.Name(rawValue: "payResult"),object: self,userInfo: dic)
//
//                             self.dismiss(animated: true, completion: nil)
//                        }
//                    }
//                }
//            }
//            if isIntercepted { self.payWebView.isHidden = true; return false}
//        } else { return false }
        
        return true
    }
    
    func payResultDeal(code:String)-> Bool {
        return code == "9000"
    }

}


extension UIViewController {

func showToast(message : String, font: UIFont) {

    let toastLabel = UILabel(frame: CGRect(x: self.view.frame.size.width/2 - 75, y: self.view.frame.size.height-100, width: 150, height: 35))
    toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
    toastLabel.textColor = UIColor.white
    toastLabel.font = font
    toastLabel.textAlignment = .center;
    toastLabel.text = message
    toastLabel.alpha = 1.0
    toastLabel.layer.cornerRadius = 10;
    toastLabel.clipsToBounds  =  true
    self.view.addSubview(toastLabel)
    UIView.animate(withDuration: 4.0, delay: 0.1, options: .curveEaseOut, animations: {
         toastLabel.alpha = 0.0
    }, completion: {(isCompleted) in
        toastLabel.removeFromSuperview()
    })
} }
